﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public partial class main : System.Web.UI.Page
{
    int pageNums = 0;
    int counts = 0;
    int currentPage = 1;
    int pageSize = 10;
    string username = "";
    NVR.BLL.DeviceTable dt = new NVR.BLL.DeviceTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        ((Label)this.Master.FindControl("lblsb")).Text = "设备搜索";
        if (!IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("default.aspx");
            }
            username = Session["username"].ToString();
            List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(currentPage, pageSize, out counts, out pageNums, username);
            if (Request.QueryString["sn"] == null)
            {
                grvTable.DataSource = list;
                grvTable.DataBind();
            }
            else
            {
                string sn = Request.QueryString["sn"].ToString();
                grvTable.DataSource = dt.GetDeviceTable(sn);
                grvTable.DataBind();

            }
            lblCounts.Text = counts.ToString();
            lblPageNums.Text = pageNums.ToString();
            lblCurrentPage.Text = currentPage.ToString();
        }
    }

    protected void preBtn_Click(object sender, ImageClickEventArgs e)
    {
        CssStyleCollection css = this.grvTable.Style;
        currentPage = Convert.ToInt32(lblCurrentPage.Text);
        if (currentPage>1)
        {
            currentPage--;
        }
        List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(currentPage, pageSize, out counts, out pageNums, Session["username"].ToString());
        lblCounts.Text = counts.ToString();
        lblPageNums.Text = pageNums.ToString();
        lblCurrentPage.Text = currentPage.ToString();
        grvTable.DataSource = list;
        grvTable.DataBind();
    }
    protected void headBtn_Click(object sender, ImageClickEventArgs e)
    {
        List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(1, pageSize, out counts, out pageNums, Session["username"].ToString());
        lblCounts.Text = counts.ToString();
        lblPageNums.Text = pageNums.ToString();
        lblCurrentPage.Text = "1";
        grvTable.DataSource = list;
        grvTable.DataBind();
    }
    protected void nextBtn_Click(object sender, ImageClickEventArgs e)
    {
        currentPage = Convert.ToInt32(lblCurrentPage.Text);
        pageNums = Convert.ToInt32(lblPageNums.Text);
        if (currentPage<pageNums)
        {
            currentPage++;
        }
        List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(currentPage, pageSize, out counts, out pageNums, Session["username"].ToString());
        lblCounts.Text = counts.ToString();
        lblPageNums.Text = pageNums.ToString();
        lblCurrentPage.Text = currentPage.ToString();
        grvTable.DataSource = list;
        grvTable.DataBind();
    }
    protected void tailBtn_Click(object sender, ImageClickEventArgs e)
    {
        pageNums = Convert.ToInt32(lblPageNums.Text);
        List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(pageNums, pageSize, out counts, out pageNums, Session["username"].ToString());
        lblCounts.Text = counts.ToString();
        lblPageNums.Text = pageNums.ToString();
        lblCurrentPage.Text = pageNums.ToString();
        grvTable.DataSource = list;
        grvTable.DataBind();
    }
    protected void shiftBtn_Click(object sender, ImageClickEventArgs e)
    {
        string strPattern="^[1-9]*[1-9][0-9]*$";
        Regex regex = new Regex(strPattern);
        if (regex.IsMatch(txtshiftpage.Text)&&txtshiftpage.Text!="")
        {
            int shiftnum = Convert.ToInt32(txtshiftpage.Text);
            pageNums = Convert.ToInt32(lblPageNums.Text);
            if (shiftnum <= pageNums && shiftnum >= 1)
            {
                List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(shiftnum, pageSize, out counts, out pageNums, Session["username"].ToString());
                lblCounts.Text = counts.ToString();
                lblPageNums.Text = pageNums.ToString();
                lblCurrentPage.Text = shiftnum.ToString();
                grvTable.DataSource = list;
                grvTable.DataBind();
            }
        }

    }

    /// <summary>
    /// 得到链接url
    /// </summary>
    /// <returns></returns>
    private string GetVedioUrl(string sn)
    {
        string vedioUrl = "";
        NVR.BLL.DeviceInfo dev = new NVR.BLL.DeviceInfo();
        NVR.BLL.NVRDevInfo nvr = new NVR.BLL.NVRDevInfo();
        NVR.Entity.DeviceInfo devInfo=dev.GetDeviceInfo(sn);
        NVR.Entity.NVRDeviceInfo nvrInfo = nvr.GetNVRInfoBySn(sn);
        if (nvrInfo == null)
        {
            string scriptTxt = "<script type='text/javascript'>alert('请您添加访问设备的用户名和密码')</script>";
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", scriptTxt);
        }
        else
        {
            string Remote_Host=Request.ServerVariables["Remote_Host"].ToString();
           // if

        }
        return vedioUrl;
       /* string hostName = Dns.GetHostName();
        if (this.txtszSerialNo.Text.Length == 0)
        {
            JScript.Alert("请输入设备序列号");
        }
        else if (this.txtlpUserName.Text.Length == 0)
        {
            JScript.Alert("请输入设备用户名");
        }
        else
        {
            string Remote_Host = base.Request.ServerVariables["Remote_Host"].ToString();
            this.strSql = new StringBuilder(100);
            this.strSql.Append(" select * from DeviceInfo_v where Sn = '" + this.txtszSerialNo.Text.Trim() + "'");
            DataTable dt = DataBase.ExecuteDataSet(this.strSql.ToString()).Tables[0];
            if (dt.Rows.Count <= 0)
            {
                JScript.Alert("没有该序列号设备");
            }
            else if (dt.Rows[0]["DeviceStatus"].ToString() == "2")
            {
                JScript.Alert("设备不在线");
            }
            else
            {
                if (dt.Rows[0]["IsCenterPreveiw"].ToString() == "1")
                {
                    this.lpManagerHostIpV4 = base.Request.ServerVariables["SERVER_NAME"].ToString();
                    this.wManagerHostPort = ConfigurationManager.AppSettings["wManagerHostPort"].ToString();
                    IPAddress[] IPs = Dns.GetHostAddresses(this.lpManagerHostIpV4);
                    if (IPs.Length > 0)
                    {
                        this.lpManagerHostIpV4 = IPs[0].ToString();
                    }
                    if (dt.Rows[0]["STSServeIsUse"].ToString() == "1")
                    {
                        this.lpManagerHostIpV4 = dt.Rows[0]["STSServerIP"].ToString();
                        this.wManagerHostPort = dt.Rows[0]["STSServerPort"].ToString();
                    }
                }
                else
                {
                    if (dt.Rows[0]["RemoteIPv4"].ToString() == Remote_Host)
                    {
                        this.lpDVRIpV4 = dt.Rows[0]["LocalIpv4"].ToString();
                        this.wDVRPort = dt.Rows[0]["RemotePort"].ToString();
                    }
                    else
                    {
                        this.lpDVRIpV4 = dt.Rows[0]["RemoteIPv4"].ToString();
                        this.wDVRPort = dt.Rows[0]["RemotePort"].ToString();
                    }
                    this.lpDVRIpV4 = dt.Rows[0]["RemoteIPv4"].ToString();
                    this.wDVRPort = dt.Rows[0]["RemotePort"].ToString();
                }
                this.lpUserName = this.txtlpUserName.Text.Trim();
                this.lpPassword = this.txtlpPassword.Text.Trim();
                this.szSerialNo = this.txtszSerialNo.Text.Trim();
                base.Response.Redirect("video.htm?lpUserName=" + this.lpUserName + "&lpPassword=" + this.lpPassword + "&lpDVRIpV4=" + this.lpDVRIpV4 + "&lpManagerHostIpV4=" + this.lpManagerHostIpV4 + "&wDVRPort=" + this.wDVRPort + "&wManagerHostPort=" + this.wManagerHostPort + "&szSerialNo=" + this.szSerialNo, true);
            }
        }*/
    }

    private void MessageBox(string key,string msg)
    {
        string scriptText = "<script type='text/javascript'>" + msg + "</script>";
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),key, scriptText);
    }
    protected void grvTable_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string str = e.Row.Cells[1].Text;
        if (e.Row.RowIndex>= 0)
        {
            e.Row.Cells[1].Attributes["onclick"] = "javascript:getVedio('" + ((HyperLink)e.Row.Cells[1].Controls[0]).Text + "')";
            e.Row.Cells[1].Attributes["onmouseover"] = "this.style.backgroundColor='#fff000'";
            e.Row.Cells[1].Attributes["onmouseout"] = "this.style.backgroundColor='transparent'";
            e.Row.Cells[1].Attributes["style"] ="cursor:hand";
        }
    }
    /*protected void linkBtnDel_Click(object sender, EventArgs e)
    {
        //this.linkBtnDel.Attributes.Add("onclick", "javascript:return " + "confirm(Are you sure you want to delete this record " + DataBinder.Eval(grvTable.Rows[1], "编号") + ")"); 
        NVR.BLL.NVRDevInfo nvr = new NVR.BLL.NVRDevInfo();
        NVR.BLL.DeviceInfo dev = new NVR.BLL.DeviceInfo();
        for (int i = 0; i < grvTable.Rows.Count; i++)
        {
            CheckBox cbx = (CheckBox)grvTable.Rows[i].FindControl("cbxId");
            if (cbx.Checked == true)
            {
                string sn = ((HyperLink)grvTable.Rows[i].Controls[1].Controls[0]).Text;
                dev.Delete(sn);
                nvr.Delete(sn);
            }
        }
        Response.Redirect("main.aspx");
    }*/
}
